package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.EmailBean;
import com.project.bean.ReceiveBean;
import com.project.bean.UserBean;
import com.project.mapper.IReceiveMapper;
import com.project.mapper.ISendMapper;
import com.project.mapper.IWriteMapper;
import com.project.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Transactional
@Service
public class EmailServiceImpl implements IEmailService {
    @Autowired
    private IWriteMapper writeMapper;
    @Autowired
    private ISendMapper sendMapper;
    @Autowired
    private IReceiveMapper receiveMapper;


    /**
     * 添加电子邮件
     *
     * @param email    电子邮件对象
     * @param userList 用户列表
     */
    @Override
    public void addEmail(EmailBean email, List<String> userList,List<Integer> IdList) {
        String joined = String.join(",",userList);
        email.setAccepters(joined);
        sendMapper.insert(email);
        sendMapper.InsertReceives(email.getId(),IdList);
    }

    @Override
    public EmailBean sendFindEmail(Integer emailId) {

        EmailBean email= sendMapper.selectById(emailId);
        return email;
    }

    /**
     * 发件箱删除邮件
     *
     * @param email 邮件对象
     */
    @Override
    public void sendDel(EmailBean email) {
        UpdateWrapper<EmailBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("pk_emailId",email.getId());
        wrapper.eq("fk_authorId",email.getAuthor());
        wrapper.set("e_delStatus","已删除");
        sendMapper.update(null,wrapper);
    }

    @Override
    public IPage<EmailBean> sendCutByItems(String theme, LocalDate startDate, LocalDate endDate,Integer authorId,Integer pageNO) {
        QueryWrapper<EmailBean> wrapper = new QueryWrapper<>();
        wrapper.eq("e_delStatus","未删除");
        wrapper.eq("fk_authorId",authorId);
        if (theme != null && theme.length() > 0){
            wrapper.like("e_theme",theme);
        }
        if (startDate != null){
            wrapper.ge("e_sendDate",startDate);
        }
        if (endDate != null){
            wrapper.le("e_sendDate",endDate);
        }

        return sendMapper.selectPage(new Page<>(pageNO,3),wrapper);
    }


    @Override
    public IPage<ReceiveBean> cutByItem(Integer pageNO, String theme, LocalDate startDate, LocalDate endDate, Integer userId) {
        return receiveMapper.cutByItem(new Page<>(pageNO, 3), theme, startDate, endDate, userId);
    }

    @Override
    public EmailBean receiveReadEmail(Integer emailId, Integer receiverId) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("fk_emailId",emailId);
        updateWrapper.eq("fk_receiverId",receiverId);
        updateWrapper.set("re_readStatus","已阅读");
        receiveMapper.update(null,updateWrapper);
        return receiveMapper.receiveReadEmail(emailId, receiverId);
    }

    @Override
    public void receiveDel(Integer emailId, Integer receiverId) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("fk_emailId",emailId);
        wrapper.eq("fk_receiverId",receiverId);
        wrapper.set("re_delStatus","已删除");
        receiveMapper.update(null,wrapper);
    }


}
